package com.wanglibao.woodtrade.woodtrade.ui.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wanglibao.woodtrade.woodtrade.R;
import com.wanglibao.woodtrade.woodtrade.ui.fragment.DemandFragment;
import com.wanglibao.woodtrade.woodtrade.ui.fragment.SupplyFragment;
import com.wanglibao.woodtrade.woodtrade.util.SPUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/*供需信息的界面 使用viewpager加载两个fragment*/
public class SupplyAndDemendActivity extends FragmentActivity implements View.OnClickListener {

    @Bind(R.id.imgbtn_commonTitle_back)
    ImageButton mImgbtnCommonTitleBack;
    @Bind(R.id.tv_chushou_title_activity)
    TextView mTvChushouTitleActivity;
    @Bind(R.id.tv_qiugou_title_activity)
    TextView mTvQiugouTitleActivity;
    @Bind(R.id.tv_title_top_activity)
    TextView mTvTitleActivity;
    @Bind(R.id.imgbtn_commonTitle_right)
    ImageButton mImgbtnCommonTitleRight;
    @Bind(R.id.rl_title_supplyAndDemand_activity)
    RelativeLayout mRlTitleSupplyAndDemandActivity;
    @Bind(R.id.vp_supplyAndDemand_activity)
    ViewPager mVpSupplyAndDemandActivity;
    @Bind(R.id.tv_baiban_left_activity)
    TextView mTvBaibanLeftActivity;
    @Bind(R.id.tv_baiban_right_activity)
    TextView mTvBaibanRightActivity;
    //==========================================
    SupplyFragment mSupplyFragment;
    DemandFragment mDemandFragment;
    List<Fragment> mFragmentList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_supply_and_demend);
        ButterKnife.bind(this);
        if (Build.VERSION.SDK_INT >= 19) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        } else {
            mRlTitleSupplyAndDemandActivity.setPadding(0, 0, 0, 0);
        }
        initData();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        String releaseType = (String) SPUtil.get(this,"ReleaseType","");
        if (releaseType.equals(ReleaseInfoActivity.TYPE_DEMAND)){
            mVpSupplyAndDemandActivity.setCurrentItem(0);
            SPUtil.remove(this,"ReleaseType");
        }else if (releaseType.equals(ReleaseInfoActivity.TYPE_SUPPLY)){
            mVpSupplyAndDemandActivity.setCurrentItem(1);
            SPUtil.remove(this,"ReleaseType");
        }

    }

    private void initData() {
        mSupplyFragment = new SupplyFragment();
        mDemandFragment = new DemandFragment();

        mTvChushouTitleActivity.setOnClickListener(this);
        mTvQiugouTitleActivity.setOnClickListener(this);
        mImgbtnCommonTitleBack.setOnClickListener(this);
        mImgbtnCommonTitleRight.setOnClickListener(this);
        mFragmentList.add(mSupplyFragment);
        mFragmentList.add(mDemandFragment);
        FragAdapter fragAdapter = new FragAdapter(getSupportFragmentManager(), mFragmentList);
        mVpSupplyAndDemandActivity.setAdapter(fragAdapter);
        mVpSupplyAndDemandActivity.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }
            @Override
            public void onPageSelected(int position) {
                if (position == 0) {
                    mTvBaibanLeftActivity.setVisibility(View.VISIBLE);
                    mTvQiugouTitleActivity.setTextColor(getResources().getColor(R.color.white));
                    mTvBaibanRightActivity.setVisibility(View.INVISIBLE);
                    mTvChushouTitleActivity.setTextColor(getResources().getColor(R.color.unSelect));
                }else {
                    mTvBaibanLeftActivity.setVisibility(View.INVISIBLE);
                    mTvQiugouTitleActivity.setTextColor(getResources().getColor(R.color.unSelect));
                    mTvBaibanRightActivity.setVisibility(View.VISIBLE);
                    mTvChushouTitleActivity.setTextColor(getResources().getColor(R.color.white));
                }

            }
            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //出售信息
            case R.id.tv_chushou_title_activity:
                mVpSupplyAndDemandActivity.setCurrentItem(1);
                break;
            //求购信息
            case R.id.tv_qiugou_title_activity:
                mVpSupplyAndDemandActivity.setCurrentItem(0);
                break;
            case R.id.imgbtn_commonTitle_right:
                startActivityForResult(new Intent(this, ReleaseActivity.class),10);
                break;
            case R.id.imgbtn_commonTitle_back:
                finish();
                overridePendingTransition(R.anim.extre_in, R.anim.extre_out);
                break;
            default:
                break;
        }
    }

    class FragAdapter extends FragmentPagerAdapter {

        private List<Fragment> mFragments;

        public FragAdapter(FragmentManager fm, List<Fragment> fragments) {
            super(fm);
            // TODO Auto-generated constructor stub  
            mFragments = fragments;
        }

        @Override
        public Fragment getItem(int arg0) {
            // TODO Auto-generated method stub  
            return mFragments.get(arg0);
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub  
            return mFragments.size();
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
